//
//  SXPChancelView.swift
//  weibo
//
//  Created by shixinPeng on 16/3/8.
//  Copyright © 2016年 shixinPeng. All rights reserved.
//

import UIKit
/*
下部四个button,显示四种表情
*/
//使用枚举,进行标签的标记识别
enum SXPChancelViewButtonType: Int{
    case Recent = 1000
    case Default = 1001
    case Emoji = 1002
    case Lxh = 1003
    
}
//创建代理
protocol SXPChancelViewDelegate: NSObjectProtocol{
    //设置代理方法
    func exchangeEmotionView(tag:Int)
}
class SXPChancelView: UIView {
    
    //设置一个代理属性
    weak var delegate:SXPChancelViewDelegate?
    //重写init方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //私有方法,添加控件和添加约束
    private func setupUI(){
      
        addSubview(recentButton)
        recentButton.tag = SXPChancelViewButtonType.Recent.rawValue
        recentButton.selected = true
        addSubview(deafautButton)
        deafautButton.tag = SXPChancelViewButtonType.Default.rawValue
        addSubview(emojiButton)
        emojiButton.tag = SXPChancelViewButtonType.Emoji.rawValue
        addSubview(lxhButton)
        lxhButton.tag = SXPChancelViewButtonType.Lxh.rawValue
        
        //添加约束
        recentButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.height.equalTo(self.snp_height)
            make.top.equalTo(self.snp_top)
            make.right.equalTo(deafautButton.snp_left)
            
        }
        deafautButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(recentButton.snp_top)
            make.left.equalTo(recentButton.snp_right)
            make.height.equalTo(recentButton.snp_height)
            make.width.equalTo(recentButton.snp_width)
        }
        emojiButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(deafautButton.snp_top)
            make.left.equalTo(deafautButton.snp_right)
            make.height.equalTo(deafautButton.snp_height)
            make.width.equalTo(deafautButton.snp_width)
        }
        lxhButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(emojiButton.snp_top)
            make.left.equalTo(emojiButton.snp_right)
            make.right.equalTo(self.snp_right)
            make.width.equalTo(emojiButton.snp_width)
            make.height.equalTo(emojiButton.snp_height)
        }
        
        
    }
    //最近按钮
    lazy var recentButton:UIButton = UIButton(title: "最近", imageStr: "compose_emotion_table_normal", SelectImageStr: "compose_emotion_table_mid_selected", targer:self, action:"changeEmoticon:")
    //默认
    lazy var deafautButton:UIButton = UIButton(title: "默认", imageStr: "compose_emotion_table_normal", SelectImageStr: "compose_emotion_table_mid_selected", targer: self, action: "changeEmoticon:")
    //emoji
    lazy var emojiButton:UIButton = UIButton(title: "emoji", imageStr: "compose_emotion_table_normal", SelectImageStr: "compose_emotion_table_mid_selected", targer: self, action: "changeEmoticon:")
    
    //浪小花
    lazy var lxhButton:UIButton = UIButton(title: "浪小花", imageStr: "compose_emotion_table_normal", SelectImageStr: "compose_emotion_table_mid_selected", targer: self, action: "changeEmoticon:")
    

    //按钮点击的触发的方法
     @objc private func changeEmoticon(button:UIButton) {
        //printLog("表情按钮被点击了")
        //关掉原有的按钮
        for subView in self.subviews{
            let btn = subView as! UIButton
                btn.selected = false
        }
        //点击打开的按钮
        button.selected = true
        
        //使用代理完成逆向传递
        delegate?.exchangeEmotionView(button.tag)
        
        
    }
    //界面滑动触发的方法
    func exchangLabel(Otag:Int ){
        //关掉原有的按钮

        var index = 0
        for subView in self.subviews{
            let btn = subView as! UIButton
            btn.selected = false
            
            if index == Otag{
                btn.selected = true
            }
            index++
        }

    }
}
